home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7182 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.5 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help : Printf() format %E !
  5. Date: 16 Feb 96 18:30:47 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Message-ID: <danpop.824495447@rscernix>
  8. References: <4g18nf$jps@ias2.ichange.com>
  9. NNTP-Posting-Host: ues5.cern.ch
  10. X-Newsreader: NN version 6.5.0 #7 (NOV)
  11.  
  12. In <4g18nf$jps@ias2.ichange.com> akmp@mindware.soft.net (anil kumar m p) writes:
  13.  
  14. >i am involved converting programs written in Fortran
  15. >onto C.
  16. >
  17. >one problem that i am unable to solve is printing exponential
  18. >values in C.
  19. >
  20. >Fortran prints exponential values with 0.4567E02
  21. >whereas C prints the same as 4.5670E01.
  22. >
  23. >How do i make C print in 0.somethingEsomething format.
  24.  
  25. How do you make Fortran print in X.somethingEsomething format? :-)
  26.  
  27. The %E format cannot do what you need, by definition.  So, you have to
  28. split the number into mantissa and exponent and printf them separately.
  29.  
  30. You can do this either mathematically or by playing with stdio functions:
  31.  
  32.   double mantissa;
  33.   int exponent;
  34.  
  35.   sprintf(buff, "%E", 0.4567E02);
  36.   *strchr(buff, 'E') = ' ';
  37.   sscanf(buff, "%lf %d", &mantissa, &exponent);
  38.   mantissa /= 10;
  39.   if (mantissa != 0) exponent++;
  40.   else exponent = 0;
  41.   printf("%fE%+.02d\n", mantissa, exponent);
  42.  
  43. If mathematical functions are inexpensive on your system, splitting
  44. the number into mantissa and exponent with log10 and pow might be
  45. more efficient.
  46.  
  47. Dan
  48. --
  49. Dan Pop
  50. CERN, CN Division
  51. Email: danpop@mail.cern.ch 
  52. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  53.